package com.jftone.bh.app.service;

import com.jftone.bh.app.config.SysConst;
import com.jftone.bh.app.data.StaticInfoQry;
import com.jftone.bh.app.model.NtbUser;
import com.jftone.bh.app.model.SysParam;
import org.apache.log4j.Logger;
import org.jftone.annotation.Autowired;
import org.jftone.annotation.Service;
import org.jftone.dao.Dao;
import org.jftone.exception.DaoException;
import org.jftone.util.DataMap;
import org.jftone.util.IData;
import org.jftone.util.StringUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class StatiService {
	
	private Logger logger = Logger.getLogger(StatiService.class);
	private IData<String, Object> block = new DataMap<String, Object>(){{
		put("value_1","");put("value_2","");put("value_3","");put("value_4","");
	}};
	private IData<String, Object> mali = new DataMap<String, Object>(){{
		put("value_1","马力");put("value_2","");put("value_3","");put("value_4","");
	}};
	private IData<String, Object> stamp = new DataMap<String, Object>(){{
		put("value_1","邮票");put("value_2","");put("value_3","");put("value_4","");
	}};
	@Autowired
	private Dao dao;


	public List<IData<String, Object>> queryStatiData(String userId, String sTime, String eTime) throws DaoException {

		List<IData<String, Object>> list = new ArrayList<IData<String, Object>>();
		IData<String, Object> param = new DataMap<String, Object>();

		if(!StringUtil.isBlank(userId)){
			param.put("USER_ID", userId);
		}
		IData<String, Object> residueData = dao.get("getUserResidue",param);
		mali.put("value_4","账户总剩余("+(residueData.getLong("MALI",0L)/100.0F)+")");
		stamp.put("value_4","账户总剩余("+(residueData.getLong("STAMP",0L)/100.0F)+")");
		if(!StringUtil.isBlank(sTime)){
			param.put("S_TIME", sTime+" 00:00:00");
		}
		if(!StringUtil.isBlank(eTime)){
			param.put("E_TIME", eTime+" 00:00:00");
		}
		List<SysParam> typeList = StaticInfoQry.getParams(SysConst.RECORD_TYPE);

		list.add(mali);
		for (SysParam sysParam:typeList) {
			short type = Short.parseShort(sysParam.getDataCode());
			if(type== SysConst.RECORD_TYPE_0 || type== SysConst.RECORD_TYPE_1 || type== SysConst.RECORD_TYPE_2
					|| type== SysConst.RECORD_TYPE_3 || type== SysConst.RECORD_TYPE_5){
				param.put("TYPE", sysParam.getDataCode());
				param.put("STATUS", SysConst.RECORD_STATUS_0);
				IData<String, Object> data;
				if(type== SysConst.RECORD_TYPE_5){
					param.put("CURRENCY", SysConst.RECORD_CURRENCY_2);
					data = dao.get("getStatiData2",param);
				}else{
					param.put("CURRENCY", SysConst.RECORD_CURRENCY_1);
					data = dao.get("getStatiData",param);
				}
				if(data!=null && !data.isEmpty()){
					list.add(new DataMap<String, Object>(){{
						put("value_1", sysParam.getDataName());put("value_2", data.getLong("VALUE_2",0)/100.0F);put("value_3", data.getLong("VALUE_3",0)/100.0F);
						put("value_4", data.getLong("VALUE_2",0)/100.0F+data.getLong("VALUE_3",0)/100.0F);
					}});
				}else {
					list.add(new DataMap<String, Object>(){{
						put("value_1", sysParam.getDataName());put("value_2", 0);put("value_3", 0);put("value_4", 0);
					}});
				}
			}
		}
		list.add(block);
		list.add(stamp);
		for (SysParam sysParam:typeList) {
			short type = Short.parseShort(sysParam.getDataCode());
			if(type== SysConst.RECORD_TYPE_2 || type== SysConst.RECORD_TYPE_3 ||type== SysConst.RECORD_TYPE_4
					|| type== SysConst.RECORD_TYPE_5 || type== SysConst.RECORD_TYPE_6){
				param.put("TYPE", sysParam.getDataCode());
				param.put("STATUS", SysConst.RECORD_STATUS_0);
				param.put("CURRENCY", SysConst.RECORD_CURRENCY_2);
				IData<String, Object> data = dao.get("getStatiData",param);
				if(data!=null && !data.isEmpty()){
					list.add(new DataMap<String, Object>(){{
						put("value_1", sysParam.getDataName());put("value_2", data.getLong("VALUE_2",0)/100.0F);put("value_3", data.getLong("VALUE_3",0)/100.0F);
						put("value_4", data.getLong("VALUE_2",0)/100.0F+data.getLong("VALUE_3",0)/100.0F);
					}});
				}else {
					list.add(new DataMap<String, Object>(){{
						put("value_1", sysParam.getDataName());put("value_2", 0);put("value_3", 0);put("value_4", 0);
					}});
				}
			}
		}
		return list;
	}
}
